// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {utils} from "../utils/utils";
import Hero from "../hero";
import GameBoard from "../game/gameboard";
import {apiUtils} from "../web/api/ApiUtils";
import {wxUtils} from "../wx/wxUtils";
import {GameMode, GameVar, GlobalObject} from "../game/GameVar";
import {WordsUtils} from "../utils/wordsUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ResultBoard extends cc.Component {

  @property(cc.Node)
  private btnShare: cc.Node = null;
  @property(cc.Node)
  private btnReturn: cc.Node = null;
  @property(cc.Node)
  private btnAgain: cc.Node = null;
  @property(cc.Label)
  private labelKill: cc.Label = null
  @property(cc.Label)
  private labelScore: cc.Label = null
  @property(cc.Label)
  private labelGain: cc.Label = null
  @property(cc.Label)
  private labelRank: cc.Label = null
  @property(cc.Node)
  private bg: cc.Node = null
  @property(cc.Node)
  private gameBoard: cc.Node = null
  @property(cc.Node)
  private extra: cc.Node = null
  @property(cc.Label)
  private labelExtra: cc.Label = null
  @property(cc.Label)
  private firstExtra: cc.Label = null
  @property(cc.Node)
  private shareSuccess: cc.Node = null


  onLoad() {
    this.setOnShare();
    utils.btnify(this.btnReturn, () => {
      cc.director.loadScene('menu')
    })
    utils.btnify(this.btnAgain, () => {
      this.close();
      this.gameBoard.getComponent(GameBoard).restart()
    })
    this.bg.on(cc.Node.EventType.TOUCH_START, (e) => {
      e.stopPropagation();
    })
  }

  private coin: number = 0;

  /**
   * 游戏结束显示面板
   * @param {Hero} hero
   * @param {number} time
   */
  show(hero: Hero, time: number) {
    this.btnShareEnable();
    this.shareSuccess.active = false
    wxUtils.showReviveAd();
    GlobalObject.voiceSource.stopAcc();
    this.node.active = true;
    this.labelScore.string = hero.score.toString()
    this.labelKill.string = hero.kill.toString()
    const rank = hero.getRank();
    if (GameVar.mode === GameMode.LIMITED) {
      this.labelRank.string = `本场第 ${rank} 名`;
      GlobalObject.voiceSource.playSettlement();
    } else {
      this.labelRank.string = '游戏结束';
    }

    const duck = utils.getAward(hero.score, rank)
    // this.coin = duck > 100 ? duck : 100
    this.coin = 5000;
    this.labelGain.string = duck.toString();
    this.firstExtra.string = `领取 ${this.coin.toString()}`

    wxUtils.postMessage({cmd: 'uploadScore', value: hero.score});
    apiUtils.gameEnd(hero.score, rank, hero.kill, time, hero.beKill)
  }

  close() {
    wxUtils.hideMenuAd()
    this.node.active = false;
  }

  setOnShare() {
    this.btnShare.active = true;
    this.extra.active = false
  }

  setShareComplete() {
    this.btnShare.active = false
    this.extra.active = true
  }

  async onShare() {
    const res = await wxUtils.shareToFriends();
    if (res) {
      // this.labelExtra.string = this.coin.toString()
      this.setShareComplete()
      apiUtils.shareNotify(2, this.coin)
      this.btnShareDisable();
      // WordsUtils.success('分享成功')
      this.shareSuccess.active = true;
      // this.btnShare.
    } else {
      WordsUtils.error('分享失败')
    }
  }

  onShareSuccessClose() {
    this.shareSuccess.active = false;
  }

  btnShareDisable() {
    this.btnShare.getComponent(cc.Button).interactable = false;
    this.btnShare.getComponentsInChildren(cc.Button).forEach((value, index) => {
      value.interactable = false;
    })
    this.btnShare.getComponentInChildren(cc.LabelOutline).color = new cc.Color().fromHEX('#8a8583')
  }

  btnShareEnable() {
    this.btnShare.getComponent(cc.Button).interactable = true;
    this.btnShare.getComponentsInChildren(cc.Button).forEach((value, index) => {
      value.interactable = true;
    })
    this.btnShare.getComponentInChildren(cc.LabelOutline).color = new cc.Color().fromHEX('#B78730')
  }

}
